﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace SynoManager.FileStation.Gui
{
    public partial class SelectRemoteFolderDialog : Form
    {
        private readonly FileStationService service;
        private const string openFolderIconKey = "$OPENFOLDER$";
        private const string closedFolderIconKey = "$CLOSEDFOLDER$";

        public SelectRemoteFolderDialog()
        {
            InitializeComponent();

            imageList.Images.Add(closedFolderIconKey, IconHelper.GetFolderIcon(IconHelper.IconSize.Small, IconHelper.FolderType.Closed));
            imageList.Images.Add(openFolderIconKey, IconHelper.GetFolderIcon(IconHelper.IconSize.Small, IconHelper.FolderType.Open));
        }

        public SelectRemoteFolderDialog(FileStationService service)
            :this()
        {
            this.service = service;
        }

        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);
            LoadRoot();
        }

        private void LoadRoot()
        {
            treeView.Nodes.Clear();
            DeviceRoot root = service.GetRoot();
            var bwa = new BackgroundWorkerArguments(treeView.Nodes, root);
            backgroundWorker.RunWorkerAsync(bwa);
        }

        class BackgroundWorkerArguments
        {
            public TreeNodeCollection ParentCollection { get; set; }
            public DirectoryInfo Directory { get; set; }
            public TreeNode[] NewNodes { get; set; }

            public BackgroundWorkerArguments(TreeNodeCollection parentCollection, DirectoryInfo directory)
            {
                ParentCollection = parentCollection;
                Directory = directory;
            }
        }

        public const string loadingNodeTag = "$$__LOADING__$$";
        private void backgroundWorker_DoWork(object sender, DoWorkEventArgs e)
        {
            var args = (BackgroundWorkerArguments)e.Argument;
            DirectoryInfo[] directories = args.Directory.GetDirectories();
            List<TreeNode> newNodes = new List<TreeNode>();
            foreach(var directory in directories)
            {
                TreeNode node = new TreeNode(directory.Name, 0, 1);
                node.Tag = directory;
                node.Nodes.Add("Loading...").Tag = loadingNodeTag;
                newNodes.Add(node);
            }
            args.NewNodes = newNodes.ToArray();
            e.Result = args;
        }

        private void backgroundWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            var args = (BackgroundWorkerArguments)e.Result;
            args.ParentCollection.Clear();
            args.ParentCollection.AddRange(args.NewNodes);
        }

        public DirectoryInfo SelectedPath
        {
            get
            {
                return treeView.SelectedNode.Tag as DirectoryInfo;
            }
        }

        public bool Overwrite
        {
            get { return overwriteCheckBox.Checked; }
            set { overwriteCheckBox.Checked = value;}
        }

        public bool ShowOverwrite
        {
            get { return overwriteCheckBox.Visible;}
            set { overwriteCheckBox.Visible = value;}
        }

        private void okButton_Click(object sender, EventArgs e)
        {
            DialogResult = DialogResult.OK;
        }

        private void treeView_AfterExpand(object sender, TreeViewEventArgs e)
        {
            if ((e.Node.Nodes.Count == 1) && ((string) e.Node.Nodes[0].Tag == loadingNodeTag))
            {
                var bwa = new BackgroundWorkerArguments(e.Node.Nodes, (DirectoryInfo) e.Node.Tag);
                backgroundWorker.RunWorkerAsync(bwa);
            }
        }
    }
}
